<?php

declare(strict_types=1);

namespace App\Controller\Kpi;

use Hyperf\HttpServer\Annotation\AutoController;
use App\Service\SysSetting as SysSettingService;

#[AutoController]
class SysSettingController extends AbstractController
{
    // 获取存储配置项
    public function get_storage()
    {
        $key = $this->request->input('key', 'local');
        $result = (new SysSettingService())->getSettingItem($key);
        return $this->returnSuccess($result, '成功');
    }

    // 保存存储配置项
    public function save_storage()
    {
        $key = $this->request->input('key', '');
        if (!$key) {
            return $this->returnError('key参数错误');
        }
        $data = $this->request->all();
        $result = (new SysSettingService())->saveSetting($data);
        if (!$result) {
            return $this->returnError('保存失败');
        }

        // 如果开启存储则修改默认存储配置
        if ($data['status'] == true) {
            (new SysSettingService())->saveSetting(['key' => 'default_storage', 'describe' => '默认存储', 'value' => $key]);
        } else {
            $default_storage = (new SysSettingService())->getSettingItem('default_storage');
            if ($default_storage && $default_storage['value'] == $key) {
                (new SysSettingService())->saveSetting(['key' => 'default_storage', 'describe' => '默认存储', 'value' => '']);
            }
        }
        return $this->returnSuccess($result, '保存成功');
    }

    // 获取站点配置项
    public function get_site()
    {
        $result = (new SysSettingService())->getSettingItem('site');
        return $this->returnSuccess($result, '保存成功');
    }

    // 保存站点配置项
    public function save_site()
    {
        $key = $this->request->input('key', '');
        if ($key != 'site') {
            return $this->returnError('key参数错误');
        }
        $data = $this->request->all();
        $data['describe'] = '站点基础信息';
        $result = (new SysSettingService())->saveSetting($data);
        return $this->returnSuccess($result, '保存成功');
    }

    // 获取站点配置项
    public function get_footer()
    {
        $result = (new SysSettingService())->getSettingItem('footer');
        return $this->returnSuccess($result, '保存成功');
    }

    // 保存站点配置项
    public function save_footer()
    {
        $key = $this->request->input('key', '');
        if ($key != 'footer') {
            return $this->returnError('key参数错误');
        }
        $data = $this->request->all();
        $data['describe'] = '站点底部信息';
        $result = (new SysSettingService())->saveSetting($data);
        return $this->returnSuccess($result, '保存成功');
    }

    // 获取统计代码配置项
    public function get_jscode()
    {
        $result = (new SysSettingService())->getSettingItem('jscode');
        return $this->returnSuccess($result, '保存成功');
    }

    // 保存统计代码配置项
    public function save_jscode()
    {
        $key = $this->request->input('key', '');
        if ($key != 'jscode') {
            return $this->returnError('key参数错误');
        }
        $data = $this->request->all();
        $data['describe'] = 'JS统计代码';
        $result = (new SysSettingService())->saveSetting($data);
        return $this->returnSuccess($result, '保存成功');
    }
}
